#!/usr/bin/env php
<?php
function call($method, $from, $talk)
{/*{{{*/
    $host='localhost';
    $port=80;
    $server='/sample/chat/input.php';

    $request['from']=$from;
    $request['talk']=$talk;
    $request_e=xmlrpc_encode_request($method, $request,
            array('escaping'=>array('markup'),'encoding'=>'utf-8'));
    print_r($request_e);
    print "----------------------------------------\n";

    if(($fp=fsockopen($host, $port, $errno, $errstr, 5)) === false)
    {
        print "Error: connect fail: $errno $errstr\n";
        return false;
    }
    $query="POST $server HTTP/1.0\n".
        "User_Agent: XML-RPC Client\n".
        "Host:$host.\n".
        "Content-Type:text/xml\n".
        "Content-Length:".strlen($request_e)."\n\n".
        "$request_e\n";
    if(fputs($fp, $query, strlen($query)) === false)
    {
        print "Error: write fail\n";
        return false;
    }
    $response_e='';
    while(!feof($fp))
    {
        $response_e.=fgets($fp);
    }
    fclose($fp);
    print_r($response_e);
    print "----------------------------------------\n";

    $xml=strstr($response_e, '<?xml');
    $response=xmlrpc_decode($xml);
    print_r($response);
    print "\n";
    print "========================================\n";
}/*}}}*/

function main($argv)
{/*{{{*/
    if(isset($argv[1]))
        $from=$argv[1];
    else
        $from='阿隆';
    if(isset($argv[2]))
        $talk=$argv[2];
    else
        $talk="Hello";
    call('INPUT', $from, $talk);
}/*}}}*/

$code=main($argv);
exit($code);
?>
